﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Excel.Expressions.Interfaces;
using Expressions.Compiler;

namespace Excel.Expressions.Test
{
    /// <summary>
    /// Summary description for Expression
    /// </summary>
    [TestClass]
    public class Expression
    {
        public Expression()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void ExpressionTestMethod1()
        {
            // Forward declarations
            Nonterminal real_literal = "real-literal";
            Nonterminal exponent_part = "exponent-part";
            Nonterminal sign = "sign";
            Nonterminal real_type_suffix = "real-type-suffix";
            Nonterminal decimal_digits = "decimal-digits";
            Nonterminal decimal_digit = "decimal-digit";

            /*
            real-literal::
                sign[opt] decimal-digits . decimal-digits exponent-part[opt] real-type-suffix[opt]
                sign[opt] . decimal-digits exponent-part[opt] real-type-suffix[opt] 
                sign[opt] decimal-digits exponent-part real-type-suffix[opt] 
                sign[opt] decimal-digits real-type-suffix 
            */
            real_literal.Definition.Rules = new Rule.Stream
            {
                () => sign[true] + decimal_digits[false] + '.' + decimal_digits + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + '.' + decimal_digits[false] + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + exponent_part + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + real_type_suffix
            };

            /*
            exponent-part::
                e sign[opt] decimal-digits 
                E sign[opt] decimal-digits 
            */
            exponent_part.Definition.Rules = new Rule.Stream
            {
                () => 'e' + sign[true] + decimal_digits,
                () => 'E' + sign[true] + decimal_digits
            };

            /*
            sign:: one of
                + - 
            */
            sign.Definition.Rules = new Rule.Stream
            {
                () => '+',
                () => '-'
            };

            /*
            real-type-suffix:: one of
                F f  
             */
            real_type_suffix.Definition.Rules = new Rule.Stream
            {
                () => 'F',
                () => 'f'
            };

            /*
            decimal-digits::
                decimal-digit 
                decimal-digits decimal-digit 
            */
            decimal_digits.Definition.Rules = new Rule.Stream
            {
                () => decimal_digit + decimal_digits[true]
            };

            /*
            decimal-digit:: one of
                0 1 2 3 4 5 6 7 8 9 
            */
            decimal_digit.Definition.Rules = new Rule.Stream
            {
                () => '0',
                () => '1',
                () => '2',
                () => '3',
                () => '4',
                () => '5',
                () => '6',
                () => '7',
                () => '8',
                () => '9'
            };

            Terminal[] _ = { '(', ')', '+', '-', '*', '/' };

            Nonterminal E = "E";
            Nonterminal Eopt = "E'";

            Nonterminal T = "T";
            Nonterminal Topt = "T'";

            Nonterminal F = "F";
            Nonterminal N = "N";

            E.Definition.Rules = new Rule.Stream
            {
                () => T[false] + Eopt[true]
            };

            T.Definition.Rules = new Rule.Stream
            {
                { () => F[false] + Topt[true] }
            };

            F.Definition.Rules = new Rule.Stream
            {
                { () => N[false] },
                { () => '(' + E[false] + ')' }
            };

            N.Definition.Rules = new Rule.Stream
            {
                { () => real_literal },
            };

            Eopt.Definition.Rules = new Rule.Stream
            {
                { () => '+' + E[false] },
                { () => '-' + E[false] },
            };

            Topt.Definition.Rules = new Rule.Stream
            {
                { () => '*' + T[false] },
                { () => '/' + T[false] },
            };

            Nonterminal grammar = "G";

            grammar.Definition.Rules = new Rule.Stream 
            {
                () => E[false] + Terminal.Empty
            };

            IToken target = grammar;

            Input input = "-.5e-2f--12e5))";
            bool expected = true;
            bool actual = target.Parse(input) == false;
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ExpressionTestMethod2()
        {
            // Forward declarations
            Nonterminal real_literal = "real-literal";
            Nonterminal exponent_part = "exponent-part";
            Nonterminal sign = "sign";
            Nonterminal real_type_suffix = "real-type-suffix";
            Nonterminal decimal_digits = "decimal-digits";
            Nonterminal decimal_digit = "decimal-digit";

            /*
            real-literal::
                sign[opt] decimal-digits . decimal-digits exponent-part[opt] real-type-suffix[opt]
                sign[opt] . decimal-digits exponent-part[opt] real-type-suffix[opt] 
                sign[opt] decimal-digits exponent-part real-type-suffix[opt] 
                sign[opt] decimal-digits real-type-suffix 
            */
            real_literal.Definition.Rules = new Rule.Stream
            {
                () => sign[true] + decimal_digits[false] + '.' + decimal_digits + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + '.' + decimal_digits[false] + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + exponent_part + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + real_type_suffix
            };

            /*
            exponent-part::
                e sign[opt] decimal-digits 
                E sign[opt] decimal-digits 
            */
            exponent_part.Definition.Rules = new Rule.Stream
            {
                () => 'e' + sign[true] + decimal_digits,
                () => 'E' + sign[true] + decimal_digits
            };

            /*
            sign:: one of
                + - 
            */
            sign.Definition.Rules = new Rule.Stream
            {
                () => '+',
                () => '-'
            };

            /*
            real-type-suffix:: one of
                F f  
             */
            real_type_suffix.Definition.Rules = new Rule.Stream
            {
                () => 'F',
                () => 'f'
            };

            /*
            decimal-digits::
                decimal-digit 
                decimal-digits decimal-digit 
            */
            decimal_digits.Definition.Rules = new Rule.Stream
            {
                () => decimal_digit + decimal_digits[true]
            };

            /*
            decimal-digit:: one of
                0 1 2 3 4 5 6 7 8 9 
            */
            decimal_digit.Definition.Rules = new Rule.Stream
            {
                () => '0',
                () => '1',
                () => '2',
                () => '3',
                () => '4',
                () => '5',
                () => '6',
                () => '7',
                () => '8',
                () => '9'
            };

            Terminal[] _ = { '(', ')', '+', '-', '*', '/' };

            Nonterminal E = "E";
            Nonterminal Eopt = "E'";

            Nonterminal T = "T";
            Nonterminal Topt = "T'";

            Nonterminal F = "F";
            Nonterminal N = "N";

            E.Definition.Rules = new Rule.Stream
            {
                { () => T[false] + Eopt[true] }
            };

            T.Definition.Rules = new Rule.Stream
            {
                { () => F[false] + Topt[true] }
            };

            F.Definition.Rules = new Rule.Stream
            {
                { () => N[false] },
                { () => '(' + E[false] + ')' }
            };

            N.Definition.Rules = new Rule.Stream
            {
                { () => real_literal },
            };

            Eopt.Definition.Rules = new Rule.Stream
            {
                { () => '+' + E[false] },
                { () => '-' + E[false] },
            };

            Topt.Definition.Rules = new Rule.Stream
            {
                { () => '*' + T[false] },
                { () => '/' + T[false] },
            };

            Nonterminal grammar = "G";

            grammar.Definition.Rules = new Rule.Stream 
            {
                () => E[false] + Terminal.Empty
            };

            IToken target = grammar;

            Input input = "-.5e-2f--12e";
            bool expected = true;
            bool actual = target.Parse(input) == false;
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ExpressionTestMethod3()
        {
            // Forward declarations
            Nonterminal real_literal = "real-literal";
            Nonterminal exponent_part = "exponent-part";
            Nonterminal sign = "sign";
            Nonterminal real_type_suffix = "real-type-suffix";
            Nonterminal decimal_digits = "decimal-digits";
            Nonterminal decimal_digit = "decimal-digit";

            /*
            real-literal::
                sign[opt] decimal-digits . decimal-digits exponent-part[opt] real-type-suffix[opt]
                sign[opt] . decimal-digits exponent-part[opt] real-type-suffix[opt] 
                sign[opt] decimal-digits exponent-part real-type-suffix[opt] 
                sign[opt] decimal-digits real-type-suffix 
            */
            real_literal.Definition.Rules = new Rule.Stream
            {
                () => sign[true] + decimal_digits[false] + '.' + decimal_digits + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + '.' + decimal_digits[false] + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + exponent_part + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + real_type_suffix
            };

            /*
            exponent-part::
                e sign[opt] decimal-digits 
                E sign[opt] decimal-digits 
            */
            exponent_part.Definition.Rules = new Rule.Stream
            {
                () => 'e' + sign[true] + decimal_digits,
                () => 'E' + sign[true] + decimal_digits
            };

            /*
            sign:: one of
                + - 
            */
            sign.Definition.Rules = new Rule.Stream
            {
                () => '+',
                () => '-'
            };

            /*
            real-type-suffix:: one of
                F f  
             */
            real_type_suffix.Definition.Rules = new Rule.Stream
            {
                () => 'F',
                () => 'f'
            };

            /*
            decimal-digits::
                decimal-digit 
                decimal-digits decimal-digit 
            */
            decimal_digits.Definition.Rules = new Rule.Stream
            {
                () => decimal_digit + decimal_digits[true]
            };

            /*
            decimal-digit:: one of
                0 1 2 3 4 5 6 7 8 9 
            */
            decimal_digit.Definition.Rules = new Rule.Stream
            {
                () => '0',
                () => '1',
                () => '2',
                () => '3',
                () => '4',
                () => '5',
                () => '6',
                () => '7',
                () => '8',
                () => '9'
            };

            Terminal[] _ = { '(', ')', '+', '-', '*', '/' };

            Nonterminal E = "E";
            Nonterminal Eopt = "E'";

            Nonterminal T = "T";
            Nonterminal Topt = "T'";

            Nonterminal F = "F";
            Nonterminal N = "N";

            E.Definition.Rules = new Rule.Stream
            {
                { () => T[false] + Eopt[true] }
            };

            T.Definition.Rules = new Rule.Stream
            {
                { () => F[false] + Topt[true] }
            };

            F.Definition.Rules = new Rule.Stream
            {
                { () => N[false] },
                { () => '(' + E[false] + ')' }
            };

            N.Definition.Rules = new Rule.Stream
            {
                { () => real_literal },
            };

            Eopt.Definition.Rules = new Rule.Stream
            {
                { () => '+' + E[false] },
                { () => '-' + E[false] },
            };

            Topt.Definition.Rules = new Rule.Stream
            {
                { () => '*' + T[false] },
                { () => '/' + T[false] },
            };

            Nonterminal grammar = "G";

            grammar.Definition.Rules = new Rule.Stream 
            {
                () => E[false] + Terminal.Empty
            };

            IToken target = grammar;

            Input input = "-.5e-2f--12e5";
            bool expected = true;
            bool actual = target.Parse(input) == true;
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ExpressionTestMethod4()
        {
            // Forward declarations
            Nonterminal real_literal = "real-literal";
            Nonterminal exponent_part = "exponent-part";
            Nonterminal sign = "sign";
            Nonterminal real_type_suffix = "real-type-suffix";
            Nonterminal decimal_digits = "decimal-digits";
            Nonterminal decimal_digit = "decimal-digit";

            /*
            real-literal::
                sign[opt] decimal-digits . decimal-digits exponent-part[opt] real-type-suffix[opt]
                sign[opt] . decimal-digits exponent-part[opt] real-type-suffix[opt] 
                sign[opt] decimal-digits exponent-part real-type-suffix[opt] 
                sign[opt] decimal-digits real-type-suffix 
            */
            real_literal.Definition.Rules = new Rule.Stream
            {
                () => sign[true] + decimal_digits[false] + '.' + decimal_digits + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + '.' + decimal_digits[false] + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + exponent_part + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + real_type_suffix
            };

            /*
            exponent-part::
                e sign[opt] decimal-digits 
                E sign[opt] decimal-digits 
            */
            exponent_part.Definition.Rules = new Rule.Stream
            {
                () => 'e' + sign[true] + decimal_digits,
                () => 'E' + sign[true] + decimal_digits
            };

            /*
            sign:: one of
                + - 
            */
            sign.Definition.Rules = new Rule.Stream
            {
                () => '+',
                () => '-'
            };

            /*
            real-type-suffix:: one of
                F f  
             */
            real_type_suffix.Definition.Rules = new Rule.Stream
            {
                () => 'F',
                () => 'f'
            };

            /*
            decimal-digits::
                decimal-digit 
                decimal-digits decimal-digit 
            */
            decimal_digits.Definition.Rules = new Rule.Stream
            {
                () => decimal_digit + decimal_digits[true]
            };

            /*
            decimal-digit:: one of
                0 1 2 3 4 5 6 7 8 9 
            */
            decimal_digit.Definition.Rules = new Rule.Stream
            {
                () => '0',
                () => '1',
                () => '2',
                () => '3',
                () => '4',
                () => '5',
                () => '6',
                () => '7',
                () => '8',
                () => '9'
            };

            Terminal[] _ = { '(', ')', '+', '-', '*', '/' };

            Nonterminal E = "E";
            Nonterminal Eopt = "E'";

            Nonterminal T = "T";
            Nonterminal Topt = "T'";

            Nonterminal F = "F";
            Nonterminal N = "N";

            E.Definition.Rules = new Rule.Stream
            {
                { () => T[false] + Eopt[true] }
            };

            T.Definition.Rules = new Rule.Stream
            {
                { () => F[false] + Topt[true] }
            };

            F.Definition.Rules = new Rule.Stream
            {
                { () => N[false] },
                { () => '(' + E[false] + ')' }
            };

            N.Definition.Rules = new Rule.Stream
            {
                { () => real_literal },
            };

            Eopt.Definition.Rules = new Rule.Stream
            {
                { () => '+' + E[false] },
                { () => '-' + E[false] },
            };

            Topt.Definition.Rules = new Rule.Stream
            {
                { () => '*' + T[false] },
                { () => '/' + T[false] },
            };

            Nonterminal grammar = "G";

            grammar.Definition.Rules = new Rule.Stream 
            {
                () => E[false] + "--12e" + Terminal.Empty
            };

            IToken target = grammar;

            Input input = "-.5e-2f--12e";
            bool expected = true;
            bool actual = target.Parse(input) == true;
            Assert.AreEqual(expected, actual);
        }


        [TestMethod]
        public void ExpressionTestMethod5()
        {
            // Forward declarations
            Nonterminal real_literal = "real-literal";
            Nonterminal exponent_part = "exponent-part";
            Nonterminal sign = "sign";
            Nonterminal real_type_suffix = "real-type-suffix";
            Nonterminal decimal_digits = "decimal-digits";
            Nonterminal decimal_digit = "decimal-digit";

            /*
            real-literal::
                sign[opt] decimal-digits . decimal-digits exponent-part[opt] real-type-suffix[opt]
                sign[opt] . decimal-digits exponent-part[opt] real-type-suffix[opt] 
                sign[opt] decimal-digits exponent-part real-type-suffix[opt] 
                sign[opt] decimal-digits real-type-suffix 
            */
            real_literal.Definition.Rules = new Rule.Stream
            {
                () => sign[true] + decimal_digits[false] + '.' + decimal_digits + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + '.' + decimal_digits[false] + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + exponent_part + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + real_type_suffix
            };

            /*
            exponent-part::
                e sign[opt] decimal-digits 
                E sign[opt] decimal-digits 
            */
            exponent_part.Definition.Rules = new Rule.Stream
            {
                () => 'e' + sign[true] + decimal_digits,
                () => 'E' + sign[true] + decimal_digits
            };

            /*
            sign:: one of
                + - 
            */
            sign.Definition.Rules = new Rule.Stream
            {
                () => '+',
                () => '-'
            };

            /*
            real-type-suffix:: one of
                F f  
             */
            real_type_suffix.Definition.Rules = new Rule.Stream
            {
                () => 'F',
                () => 'f'
            };

            /*
            decimal-digits::
                decimal-digit 
                decimal-digits decimal-digit 
            */
            decimal_digits.Definition.Rules = new Rule.Stream
            {
                () => decimal_digit + decimal_digits[true]
            };

            /*
            decimal-digit:: one of
                0 1 2 3 4 5 6 7 8 9 
            */
            decimal_digit.Definition.Rules = new Rule.Stream
            {
                () => '0',
                () => '1',
                () => '2',
                () => '3',
                () => '4',
                () => '5',
                () => '6',
                () => '7',
                () => '8',
                () => '9'
            };

            Terminal[] _ = { '(', ')', '+', '-', '*', '/' };

            Nonterminal E = "E";
            Nonterminal Eopt = "E'";

            Nonterminal T = "T";
            Nonterminal Topt = "T'";

            Nonterminal F = "F";
            Nonterminal N = "N";

            E.Definition.Rules = new Rule.Stream
            {
                { () => T[false] + Eopt[true] }
            };

            T.Definition.Rules = new Rule.Stream
            {
                { () => F[false] + Topt[true] }
            };

            F.Definition.Rules = new Rule.Stream
            {
                { () => N[false] },
                { () => '(' + E[false] + ')' }
            };

            N.Definition.Rules = new Rule.Stream
            {
                { () => real_literal },
            };

            Eopt.Definition.Rules = new Rule.Stream
            {
                { () => '+' + E[false] },
                { () => '-' + E[false] },
            };

            Topt.Definition.Rules = new Rule.Stream
            {
                { () => '*' + T[false] },
                { () => '/' + T[false] },
            };

            Nonterminal grammar = "G";

            grammar.Definition.Rules = new Rule.Stream 
            {
                () => E[false]
            };

            IToken target = grammar;

            Input input = "-.5e-2f--12e";
            bool expected = true;
            bool actual = target.Parse(input) == true;
            Assert.AreEqual(expected, actual);
        }
    }
}
